/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package riteservices.services;

import JSON.JSONArray;
import JSON.JSONObject;
import com.visural.common.IOUtil;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import riteservices.model.bean.Fbfriends;
import riteservices.model.bean.User;
import riteservices.model.dao.FbFriendsDAO;
import riteservices.model.dao.UserDAO;
import riteservices.util.Constantes;

/**
 *
 * @author sixviv
 */
public class UserSrv {
    public UserDAO uDAO = new UserDAO();
    

    public String updateUserData(User user){
        if(uDAO.update(user)){
            return Constantes.TRUE_STR;
        }else{
            return Constantes.FALSE_STR;
        }
    }

    public User getUserDetails(int idUser){
        return uDAO.getUserById(idUser);
    }

    public String deleteUserAccount(User user){
        if(uDAO.delete(user)){
            return Constantes.TRUE_STR;
        }else{
            return Constantes.FALSE_STR;
        }
    }
    
    public User userLogin(String email,String password){
        User user = null;
        if(userExist(email)){
            user=uDAO.getUserData(email);
            if(user.getPassword().compareTo(password)==0){
                return user;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    private boolean userExist(String email){
        return uDAO.emailExist(email);
    }

    public boolean verifyUserData(User user){
    return emailExist(user.getEmail());
    }

    private boolean emailExist(String email) {
        return uDAO.emailExist(email);
    }

    public String createUser(User user){
        if(uDAO.add(user)){
            return Constantes.TRUE_STR;
        }else{
            return Constantes.FALSE_STR;
        }
    }

    public User loginWithFacebook(String accessToken, int expires) {
        try {
            JSONObject resp = new JSONObject(
                IOUtil.urlToString(new URL("https://graph.facebook.com/me?access_token=" + accessToken)));
            String email = resp.getString("email");
            if(!this.emailExist(email)){
                User user = new User();
                user.setEmail(email);
                user.setFacebookId(resp.getString("id"));
                user.setLastName(resp.getString("last_name"));
                user.setName(resp.getString("first_name"));
                SimpleDateFormat formato = new SimpleDateFormat("MM/dd/yyyy");
                if(resp.has("birthday")){
                    Date birthday = formato.parse(resp.getString("birthday"));
                    user.setBirthDate(birthday);
                }else{
                    user.setBirthDate(new Date());
                }
                user.setProfileName(resp.getString("username"));
                user.setGender(resp.getString("gender"));
                user.setPassword("");
                user.setCompany("");
                user.setPhoneNumber(00000000);
                this.createUser(user);
                JSONObject friends = new JSONObject(
                IOUtil.urlToString(new URL("https://graph.facebook.com/me/friends?access_token=" + accessToken)));
                JSONArray friendsArray = friends.getJSONArray("data");
                for(int i=0; i<friendsArray.length(); i++){
                    FbFriendsDAO fDAO = new FbFriendsDAO();
                    User userFriend = uDAO.getByFBId(((JSONObject)friendsArray.get(i)).getString("id"));
                    if(userFriend!=null){
                        fDAO.add(new Fbfriends(user, userFriend));
                        fDAO.add(new Fbfriends(userFriend, user));
                    }
                }
                
                return user;
            }else{
                return this.userLogin(email, "");
            }  
        } catch (Throwable ex) {
            throw new RuntimeException("failed login", ex);
        }
    }


}
